package com.zjg.dao;

import java.io.IOException;
import java.io.InputStream;

import com.zjg.bean.User;
import com.zjg.dao.iface.RegisterDao;
import com.zjg.db.utills.JdbcTemplate;

import oracle.sql.BLOB;

public class RegisterDaoImp implements RegisterDao{

	@Override
	public int register(User user,InputStream in) {
		int rowNum=0;
		
		
		String sql0 ="insert into users values(?,?,?)";
		String sql1="insert into usermessage(user_id,sex,native,email,nikname) values(?,?,?,?,?)";
		String sql2="insert into USER_HOBBY(user_id,hobby_id) values(?,?)";
		rowNum+=JdbcTemplate.update(sql0, user.getId(),user.getUserName(),user.getPassword());
		rowNum+=JdbcTemplate.update(sql1, user.getId(),user.getSex(),user.getPlace(),user.getEmail(),user.getNikname());
		if(user.getHobby()!=null&&user.getHobby().length!=0) {
		for(int i=0;i<user.getHobby().length;i++) {
			rowNum+=JdbcTemplate.update(sql2, user.getId(),user.getHobby()[i]);
		}}
		String sql3="update usermessage set headicon=? where user_id=?";
		try {
			rowNum+=JdbcTemplate.updateHeadIcon(sql3, in, user.getId());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
			System.out.println(rowNum);	
		return rowNum;
		
	}

	@Override
	public int checkUserId(String userID) {
		int rowNum=0;
		String sql="select count(1) from users where user_id=?";
		int userid=Integer.parseInt(userID);
		rowNum=JdbcTemplate.queryForCount(sql,userid);
		return rowNum;
	}

	@Override
	public int checkUserName(String userName) {
		int rowNum=0;
		String sql="select count(1) from users where user_name=?";
		rowNum=JdbcTemplate.queryForCount(sql,userName);
		return rowNum;
		
	}
	public void setAdmin(int rolenum,int id) {
		String sql="insert into USER_ROLE values(0,?,?)";
		JdbcTemplate.update(sql, id,rolenum);
		
	}

}
